package com.yutel.util;

import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author yuanyajun
 */
public class ServletProcess {

    private static String URL = "http://192.168.3.217:8080/msweb/";

    public static String request(String parameter) throws IOException {

        String URLString = URL + "system/login?username=aaa&password=bbbb";
        HttpConnection hpc = null;
        DataInputStream dis = null;
        boolean newline = false;
        String content = "";
        try {
            hpc = (HttpConnection) Connector.open(URLString + parameter);
            hpc.setRequestMethod(HttpConnection.GET);
            dis = new DataInputStream(hpc.openInputStream());
            int character;
            while ((character = dis.read()) != -1) {
                if ((char) character == '\\') {
                    newline = true;
                    continue;
                } else {
                    if ((char) character == 'n' && newline) {
                        content += "\n";
                        newline = false;
                    } else if (newline) {
                        content += "\\" + (char) character;
                        newline = false;
                    } else {
                        content += (char) character;
                        newline = false;
                    }
                }
            }
        } catch (IOException e) {
            System.out.print("ERROR:" + e);
        } finally {
            if (hpc != null) {
                hpc.close();
                hpc = null;
            }
            if (dis != null) {
                dis.close();
            }
        }
        content = (unicode2gb2312(content)).trim();
        return content;
    }

    public static String unicode2gb2312(String s) {
        if (s == null) {
            return "";
        }
        if (s.equals("")) {
            return s;
        }
        try {
            return new String(s.getBytes("ISO8859_1"), "gb2312");
        } catch (Exception uee) {
            uee.printStackTrace();
            return s;
        }
    }
}
